#!/usr/bin/env python
# -*- coding: utf-8 -*-
# --------------------------------------------------------
# Descripttion: https://github.com/fuyi501
# version: 0.0.1
# Author: Wenwei Fu (2516800961@qq.com)
# FilePath: /manage-fastapi/fastapi-realworld-example-app-mysql/app/db/schemas.py
# Create: 2021-03-31 20:45:31
# LastAuthor: Wenwei Fu
# lastTime: 2021-04-01 10:23:39
# --------------------------------------------------------

import datetime
import uuid
from peewee import *
# from playhouse.postgres_ext import ArrayField
import playhouse.postgres_ext as pg
from app.db.database import BaseModel

# 在这里定义所有的 数据库表

# 用户
class User(BaseModel):
    id = AutoField()
    username = CharField()
    user_phone = CharField()
    email = CharField()
    user_id = CharField(default=str(uuid.uuid1()))
    salt = CharField()
    hashed_password = CharField()
    bio = CharField(default="用户_" + str(uuid.uuid1())[:6])
    image = CharField()
    created_at = DateTimeField(default=datetime.datetime.now)
    updated_at = DateTimeField(default=datetime.datetime.now)

# 道路信息表
class Traffic_Road(BaseModel):
    id = AutoField(primary_key=True)
    road_id = CharField(unique=True) # 道路ID
    road_name = CharField() # 道路名称
    road_lonlat = CharField(null=True, help_text="经纬度", verbose_name="经纬度") # 经纬度
    created_at = DateTimeField(default=datetime.datetime.now)
    updated_at = DateTimeField(default=datetime.datetime.now)

# 摄像头列表
class Traffic_Cameras(BaseModel):
    id = AutoField(primary_key=True)
    camera_id = CharField() # 摄像头ID
    camera_name = CharField() # 摄像头名称
    camera_type = CharField() # 摄像头类型
    camera_lonlat = CharField(null=True, help_text="经纬度", verbose_name="经纬度") # 经纬度
    camera_road_id = CharField() # 摄像头道路ID
    camera_position = CharField() # 安装位置
    camera_scene = CharField(null=True) # 镜头
    camera_video_url = CharField(null=True) # 视频流地址
    camera_original_image_url = CharField(null=True) # 原始图片地址
    camera_image_url = CharField(null=True) # 标注后的图片地址
    created_at = DateTimeField(default=datetime.datetime.now)
    updated_at = DateTimeField(default=datetime.datetime.now)

# 区域表
class Traffic_Area(BaseModel):
    id = AutoField()
    area_id = CharField() # 区域 ID
    area_name = CharField() # 区域名称
    area_number = IntegerField() # 区域编号
    camera_id = CharField() # 摄像头 ID
    pixel_coordinates = CharField(null=True) # 像素坐标
    geo_coordinates = CharField(null=True) # 地理坐标
    event_list = pg.ArrayField(pg.TextField, default=[], null=True) # 事件列表
    props = CharField(null=True) # 其他参数
    created_at = DateTimeField(default=datetime.datetime.now)
    updated_at = DateTimeField(default=datetime.datetime.now)
# Artist.select().where(SQL("albums = '{}'"))

# 车道表
class Traffic_Lane(BaseModel):
    id = AutoField()
    lane_id = CharField() # 车道 ID
    lane_name = CharField() # 车道名称
    lane_number = IntegerField() # 车道编号
    camera_id = CharField() # 摄像头 ID
    pixel_coordinates = CharField(null=True) # 像素坐标
    geo_coordinates = CharField(null=True) # 地理坐标
    lane_direction = CharField(null=True) # 车道方向
    max_speed = IntegerField(null=True) # 车道方向
    props = CharField(null=True) # 其他参数
    created_at = DateTimeField(default=datetime.datetime.now)
    updated_at = DateTimeField(default=datetime.datetime.now)

# 交通事件代码对应表
class Traffic_Event_Code(BaseModel):
    id = AutoField()
    event_value = CharField() # 事件类型 code
    event_label = CharField() # 事件类型名称
    created_at = DateTimeField(default=datetime.datetime.now)
    updated_at = DateTimeField(default=datetime.datetime.now)

    # 车道方向代码对应表
class Traffic_Lane_Code(BaseModel):
    id = AutoField()
    lane_value = CharField() # 车道方向 code
    lane_label = CharField() # 车道方向名称
    created_at = DateTimeField(default=datetime.datetime.now)
    updated_at = DateTimeField(default=datetime.datetime.now)

def create_tables(db):
    db.create_tables([
        User,
        Traffic_Road,
        Traffic_Cameras,
        Traffic_Area,
        Traffic_Lane,
        Traffic_Event_Code,
        Traffic_Lane_Code
    ])